<template>
  <div class="container">
    <van-sticky>
      <!-- 头部区域 -->
      <Header></Header>

      <!-- 导航区域 -->
      <van-tabs @click-tab="onClickTab" v-model:active="active" color="red" line-height="2px" line-width="70px">
        <van-tab v-for="(nav,index) in navList" :key="index" :title="nav.name"></van-tab>
      </van-tabs>

    </van-sticky>

    <div style="height:15px;background-color: #ccc;"></div>

    <!-- 内容展示区 -->
    <div class="contentBox">

      <img v-if="loading" class="loading" src="../../assets/images/loading.gif" alt="">
      
      <div v-if="!loading" class="title">{{ goodTitle }}</div>

        <div v-if="!loading" class="listBox">
          <div class="itemBox" @click="toDetail" v-for="item in goods" :key="item.id">
            <img class="itemImg" :src="item.listPicUrl" alt="">
            <div class="itemTitle">
              <span class="newGoods">新品</span>
              {{ item.name }}
            </div>
            <div class="priceBox">
              <span class="money">￥</span>
              <span>{{ item.retailPrice }}</span>
              <span class="oldPrice">￥{{ item.sortOriginPrice }}</span>
            </div>
            <div class="payBox">
              <span class="strong">特价</span>
              <span>正在抢购中</span>
            </div>
          </div>
        </div>

      </div>


  </div>
</template>

<script lang="ts" setup>
import {onMounted,ref} from "vue"
import { useRoute, useRouter } from 'vue-router';
import { reqCategoryGoods } from "../../api/category";
import { CategoryL2List, ItemList } from "../../types/category";


const route = useRoute()
const router = useRouter()

const navList = ref<CategoryL2List[]>([])
const active = ref(0)
const currentId = ref()
const goods = ref<ItemList[]>([])
const goodTitle = ref('')
const subId = ref()
const loading = ref(false);

subId.value = route.query.subId
currentId.value = route.query.id

const getGoodsList =async () => {
  /* 获取商品二级分类数据 */
  // console.log(subId.value);
  loading.value = true
  let res2 = await reqCategoryGoods(currentId.value,subId.value)
  loading.value = false
  navList.value = res2.data.categoryL2List
  goods.value = res2.data.categoryItems.itemList

  goodTitle.value = res2.data.categoryItems.category.frontDesc
  
}

/* 当切换导航的时候执行的函数 */
const onClickTab =async ({title,name}:any) => {
  /* title是当前点击的导航名称,name是当前选中导航的下标 */
  active.value = name
  /* 通过下标去导航数据中查找当前这项数据,拿到它的二级导航id */
  const currentNav = navList.value.find(item => item.name === title)
  subId.value = currentNav?.id as number

  loading.value = true 
  /* 通过拿到的ID发送请求,获取商品数据 */
  let res2 = await reqCategoryGoods(currentId.value,subId.value)
  
  navList.value = res2.data.categoryL2List
  goods.value = res2.data.categoryItems.itemList
  goodTitle.value = res2.data.categoryItems.category.frontDesc

  loading.value = false


  /* 切换导航的时候,新的页面自动滚动到顶部 */
  window.scrollTo({
    top: 0,
    behavior: 'smooth' // 可选，平滑滚动到顶部
  });

}


/* 去详情页 */
const toDetail = () => {
  router.push('/detail')
}


onMounted(()=>{
  getGoodsList()
  
})

</script>

<script lang="ts">
export default {
  name: 'goodsList'
}
</script>

<style lang="scss" scoped>
.container{
  .contentBox{
    .title{
      font-size: 14px;
      text-align: center;
      height:55px;
      line-height: 55px;
    }
    .listBox{
      padding:0 10px;
      display: flex;
      flex-wrap: wrap;
      justify-content: space-between;
      .itemBox{
        width:172px;
        margin-bottom: 20px;
        .itemImg{
          width:172px;
          height:172px;
          border-radius: 10px;
        }
        .itemTitle{
          font-size: 14px;
          display: -webkit-box;
          -webkit-box-orient: vertical;
          -webkit-line-clamp: 2;
          overflow: hidden;
          line-height: 20px;
          .newGoods{
            font-size: 10px;
            padding:3px 6px;
            background-color: orange;
            color: white;
            border-radius: 2px;
          }
        }
        .priceBox{
          font-size: 20px;
          color:red;
          font-weight: bold;
          .money{
            font-size: 14px;
          }
          .oldPrice{
            font-size: 14px;
            color:#aaa;
            text-decoration: line-through;
            font-weight:normal;
          }
        }
        .payBox{
          height:23px;
          background-color: #ffe9eb;
          color: #fa1e32;
          font-size: 12px;
          font-weight: bold;
          line-height: 23px;
          border-radius: 15px;
          .strong{
            display: inline-block;
            width:40px;
            height:19px;
            line-height: 19px;
            border-radius: 15px;
            background-color: #fa1e32;
            color:white;
            text-align: center;
            margin-right: 10px;
          }
        }
      }
    }
  }
}


.loading{
  width:100%;
}
</style>
